<?php
/**
 * Created by PhpStorm.
 * User: 张鹏
 * Initial-Time: 2017-05-04 上午 10:44
 * Versions：v1.00
 * Last-time: 2017-05-04 上午 10:44
 * Desc：计算产品价格
 */

namespace app\common\model;


use think\Db;

class OrderDiscount
{
    private function bogof($detail)
    {
        if(!$detail){
            return ['state'=>0,'detail error'];
        }
        //$info = Db::name($demand."_detail")->where("detail_id = $detail[detail_id]")->find();
        $price_total = $detail['price']*ceil($detail['amount']/2);
        return ['state'=>1,'total_fee'=>$price_total];
    }
    private function twofer($detail)
    {
        if(!$detail){
            return ['state'=>0,'detail error'];
        }
        if($detail['amount'] == 1){
            $price_total = $detail['price'];
        }else{
            $price_total = floor($detail['amount']/2)*$detail['price']/2+($detail['amount']-floor($detail['amount']/2))*$detail['price'];
        }
        return ['state'=>1,'total_fee'=>$price_total];
    }
    private function discount_none($detail)
    {
        if(!$detail){
            return ['state'=>0,'detail error'];
        }
        //$info = Db::name($demand."_detail")->where("detail_id = $detail[detail_id]")->find();
        if(isset($detail['discount']) && $detail['discount']){
            $detail['discount'] = $detail['discount']/100;
        }else{
            $detail['discount'] = 1;
        }
        $price_total = $detail['price']*$detail['amount']*$detail['discount'];
        return ['state'=>1,'total_fee'=>$price_total];
    }
    private function preference($detail)
    {
        if(!$detail){
            return ['state'=>0,'detail error'];
        }
        $detail['discount'] ? $detail['discount'] = $detail['discount']/100 : $detail['discount'] = 1;
        if($detail['member_discount'] && $detail['member_discount']<=$detail['discount']){
            $price_total = $detail['amount'] * $detail['price']*$detail['member_discount'];
        }else{
            $price_total = $detail['amount'] * $detail['price']*$detail['discount'];
        }
        return ['state'=>1,'total_fee'=>$price_total];
    }
    //计算价格

    /**
     * @param $demand
     * @param $detail
     * @param $card_code
     * @return array
     */
    public function apply($demand, $detail, $card_code)
    {
        if(!$demand){
            return ['state'=>0,'demand error'];
        }
        if(!$detail){
            return ['state'=>0,'detail error'];
        }
        $discount = '';
        if ($card_code) {
            $card = model('card')->getUserIdByCode($card_code);
            if (!$card['state']) {
                return $card;
            }
            $user_id = $card['data']['user_id'];
            $discount = model('member')->getDiscountByUserId("cafe", $user_id);
            if (!$discount['state']) {
                return $discount;
            }
        }
        $member_discount = "";
        if($discount && isset($discount['info']['discount']) &&$discount['info']['discount']){
            $member_discount = $discount['info']['discount'];
        }
        if ($discount) {
            //折扣
            $info['discount'] = $discount['info']['discount'];
            //user_name
            $user = model('user')->getInfoById($user_id);
            $info['user_name'] = $user['set_name'] ? $user['set_name'] : $user['user_name'];
            $info['tel'] = isset($user['tel']) ? $user['tel'] : "";
            //会员等级
            $info['intro'] = $discount['info']['intro'];
            //会员卡余额
            $info['balance'] = model('account')->getBalanceByUserId($user_id)['data']['balance'];
            $info['demand'] = $discount['info']['demand'];
        }
        //获取detail信息
        $detail_n = json_decode($detail, 1);
        if (!$detail_n) {
            return ['state' => 0, 'errormsg' => 'detail error'];
        }
        //获取配置文件
        $config = getConfig('orderDiscount')[$demand];
        //取出id
        $detail_key = array_keys($detail_n);
        $detail_key = implode(',', $detail_key);
        //通过id 查询咖啡信息
        $brand_info = Db::name($demand.'_detail')
            ->alias('a')
            ->join($demand.' b',"a.".$demand."_id = b.".$demand."_id")
            ->join($demand."_label c","b.labels = c.label_id")
            ->where("detail_id in ($detail_key)")->select();
        $total_fee = [];
        $price_total = [];
        $details = [];
        foreach ($brand_info as $key=>$value){
            $details['amount'] = $detail_n[$value['detail_id']];
            $details['price'] = $value['price'];
            $details['discount'] = isset($value['discount']) ? $value['discount'] : 100;
            $details['member_discount'] = $member_discount;
            if(isset($value['class']) && $value['class']){
                if(isset($config[$value['class']])&& $config[$value['class']]){
                    $prices = $this->$value['class']($details);
                    if($prices['state']){
                        $total_fee[] = $prices['total_fee'];
                    }
                }
            }else{
                if($this->preference($details)['state']){
                    $total_fee[] = $this->preference($details)['total_fee'];
                }
            }
            $price_total[] = $detail_n[$value['detail_id']] * $value['price'];
        }
        $price = array_sum($total_fee);
        if (!$price or $price<=0){
            return [
                'state' => 0,
                'errormsg' => 'reckon_price error'
            ];
        }
        //最终价格
        $info['total_fee_product'] = array_sum($price_total);
        //优惠价
        $info['discount_fee'] = array_sum($price_total) - $price;
        //实际价格
        $info['total_fee'] = $price;
        return ['state'=>1,'data'=>$info];
    }
}